<?php
/**
 * User management page controller
 *
 * @author Dima
 *
 */

class Admin_UserController extends modules_admin_controllers_ControllerBase
{
	protected $_bForLoggedUsersOnly = true;
	
//----------------------------------------------------------------------------------------------------
	
	public function init()
	{
		if (! $this->getRequest()->isXmlHttpRequest())
		{
			parent::init();
			$this->view->title = 'User Management';
		}
		
		$this->_helper->AjaxContext()->initContext('json');		
	}
	
//----------------------------------------------------------------------------------------------------

	public function indexAction()
	{
		$this->view->headLink()->appendStylesheet('/css/default.css');
		$this->view->headScript()->appendFile('/js/jquery.tablesorter.js');
		$this->view->headScript()->appendFile('/js/sort.js');				
		
		$aSelectUsers = new  models_User;
		$aSelectUsers = $aSelectUsers->listUsers();
		$paginator    = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($aSelectUsers));

		$page = $this->_getParam('page',1);
		$paginator->setItemCountPerPage(100)
				  ->setCurrentPageNumber($page);

		$this->view->paginator = $paginator;		
	}
	
//----------------------------------------------------------------------------------------------------

	public function editAction()
	{
		$this->view->headLink()->appendStylesheet('/css/user.css');
		$oRequest = $this->getRequest();
		$oForm = new modules_default_forms_UserForm();

		if ($oRequest->isPost())
		{
			$aPost = $oRequest->getPost();
//			$oForm->isValid($aPost);
			$aData = array(
					'email'				=>	strip_tags($oRequest->getParam('email')),
					'role'				=>	strip_tags($oRequest->getParam('role')) ,
					'status'			=>	strip_tags($oRequest->getParam('status')),
					'password'			=>	strip_tags($oRequest->getParam('password')),
					'session_id'		=> 	strip_tags($oRequest->getParam('session_id')),
			        'name'          	=>  strip_tags($oRequest->getParam('name')),
					'freestyle_project' =>  strip_tags($oRequest->getParam('freestyle_project')),
			);
			

			models_UserMapper::updateArray($this->_request->getParam('id'), $aData);
			
			$this->_redirect('/admin/user/');
		}
		else
		{
			$this->view->oItem = models_UserMapper::findById($this->_request->getParam('id'));
		}
	}
	
//----------------------------------------------------------------------------------------------------

	public function deleteAction()
	{
		try 
		{
			models_UserMapper::delete($this->_request->getParam('id'));
		} 
		catch (Exception $e) 
		{
		}
			$this->_redirect('/admin/user/');
	}
	
//----------------------------------------------------------------------------------------------------

    public function addAction()
    {
        $oRequest           = $this->getRequest();
        
        if ($oRequest->isPost())
        {
            $email              = strip_tags($oRequest->getParam('email'));
            $password           = strip_tags($oRequest->getParam('password'));
            $role               = strip_tags($oRequest->getParam('role'));
            $name               = strip_tags($oRequest->getParam('name'));    
            $freestyle_project  = strip_tags($oRequest->getParam('freestyle_project'));               

            $aData['email']     		= $email;
            $aData['password']  		= $password;
            $aData['role']      		= $role;
            $aData['name']      		= $name;
            $aData['freestyle_project'] = $freestyle_project;
                        
            $idItem = models_UserMapper::saveArray($aData);

            $this->_redirect('/admin/user');
        }
        else
        {
//            $this->view->headScript()->appendFile('/js/addcardcategory.js');
        }
    }
    
//----------------------------------------------------------------------------------------------------
	
}